<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      :page.sync="page"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList } from '@/api/monitor/synchronizeLog';
export default {
  data() {
    return {
      query: {
        startTime: this.getDefaultTime()[0],
        endTime: this.getDefaultTime()[1]
      },
      loading: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      option: {
        height: 'calc(100vh - 406px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        searchLabelWidth: 100,
        border: true,
        index: true,
        indexLabel: '序号',
        viewBtn: true,
        viewText: '详情',
        viewBtnText: '详情',
        editBtn: false,
        addBtn: false,
        delBtn: false,
        menu: false,
        column: [
          {
            label: '流水号',
            prop: 'serialNumber',
            search: true
          },
          {
            label: '业务数据',
            prop: 'businessData'
          },
          {
            label: '同步类型',
            prop: 'syncType',
            search: true,
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sync_scm_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '同步结果',
            prop: 'syncResult',
            search: true,
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sync_scm_result',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '同步开始时间',
            prop: 'syncStartTime'
          },
          {
            label: '同步结束时间',
            prop: 'syncEndTime'
          },
          {
            label: '堆栈异常信息',
            prop: 'stackTrace'
          },
          {
            label: '备注',
            prop: 'remark'
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            dicData: [
              {
                label: '正常',
                value: '1'
              }
            ]
          },
          {
            label: '创建时间',
            prop: 'createTime',
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间',
            searchSpan: 12,
            searchValue: this.getDefaultTime(),
            searchRules: [
              {
                trigger: 'change',
                validator: (rule, value, callback) => {
                  const endTime = value[1];
                  const startTime = value[0];
                  if (this.$moment(endTime).diff(this.$moment(startTime), 'day') > 92) {
                    callback(new Error('查询时间最多选择三个月时间'));
                  }
                  callback();
                }
              }
            ],
            search: true
          },
          {
            label: '更新时间',
            prop: 'updateTime'
          }
        ]
      },
      data: []
    };
  },
  computed: {},
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    getDefaultTime() {
      const startTime = this.$moment(new Date()).subtract(7, 'days').format('yyyy-MM-DD hh:mm:ss');
      const endTime = this.$moment(new Date()).format('yyyy-MM-DD hh:mm:ss');
      return [startTime, endTime];
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.createTime && params.createTime.length > 0) {
        params.startTime = params.createTime[0];
        params.endTime = params.createTime[1];
        delete params.createTime;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this._forEach(this.data, (item) => {
          item.syncType = String(item.syncType);
          item.status = String(item.status);
        });
        this.loading = false;
      });
    }
  }
};
</script>

<style scoped></style>
